﻿using Exam.infranstuctrue.Tools;
using Microsoft.AspNetCore.Mvc.Filters;
using System.IdentityModel.Tokens.Jwt;

namespace Exam.Filters
{
    public class AsyncSessionFilter : IAsyncActionFilter
    {
        private readonly Session _session;

        public AsyncSessionFilter(Session session)
        {
            _session = session;
        }

        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var user = context.HttpContext.User;
            _session.Uid = user.FindFirst(JwtRegisteredClaimNames.Jti)?.Value;
            await next();
        }
    }
}
